using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using UnityEngine;

namespace Game.Component
{
	public class OvenItem : ContainerItem
	{
		public bool isBurn;

		private Transform smokeEfc;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (!isBurn)
			{
				return;
			}
			for (int i = 0; i < childItems.Count; i++)
			{
				if (childItems[i].itemData.canBurn)
				{
					childItems[i].Burn(Time.deltaTime);
				}
			}
		}

		public override bool AttachItem(BaseItem item, DepthType depthType, AttachType attachType, bool updatePosition)
		{
			if (item.itemData.canBurn)
			{
				IsBurn(true);
				item.StartBurn();
			}
			return base.AttachItem(item, depthType, attachType, updatePosition);
		}

		public override void ReleaseAttachItem(BaseItem item)
		{
			base.ReleaseAttachItem(item);
			if (!isBurn || !item.itemData.canBurn)
			{
				return;
			}
			bool flag = false;
			for (int i = 0; i < childItems.Count; i++)
			{
				if (childItems[i].itemData.canBurn)
				{
					flag = true;
					break;
				}
			}
			if (!flag)
			{
				IsBurn(flag);
			}
		}

		public void IsBurn(bool val)
		{
			if (isBurn != val)
			{
				isBurn = val;
				if (isBurn)
				{
					ShowSmokeEffect();
					Singleton<AudioManager>.Instance.PlayAudio("sound_grilling", true, GetAudioPlayer());
				}
				else
				{
					SetEffectLoop(smokeEfc, false);
					RemoveAudioPlayer();
				}
			}
		}

		private void ShowSmokeEffect()
		{
			if (string.IsNullOrEmpty(base.itemData.effect))
			{
				return;
			}
			if (smokeEfc == null)
			{
				Transform transform = model.Find("Efc");
				if (transform != null)
				{
					smokeEfc = Singleton<GamePoolManager>.Instance.Spawn(PathConst.EFFECT + base.itemData.effect);
					if (smokeEfc != null)
					{
						smokeEfc.SetParent(transform);
						smokeEfc.localPosition = Vector3.zero;
						smokeEfc.localScale = Vector3.one;
					}
				}
			}
			SetEffectLoop(smokeEfc, true);
		}
	}
}
